<template>
  <div class="future-weather">
    <div>
      <h3>最后更新时间:{{ updateTime }}</h3>
    </div>
    <div class="chart-container" ref="chartContainer"></div>
  </div>
</template>

<script>
//import FutureWeather from '@/components/FutureWeather.vue'
import axios from "axios";
import * as echarts from 'echarts/core';
import { GridComponent } from 'echarts/components';
import { LineChart } from 'echarts/charts';
import { UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';

echarts.use([GridComponent, LineChart, CanvasRenderer, UniversalTransition]);
import dayjs from 'dayjs';
import { KEY, API_24H } from "../common/constrant";
// 在单独构建的版本中辅助函数为 Vuex.mapState
import { mapState } from 'vuex';

export default {
  data() {
    return {
      hourly: [],
      hour24Data: {
        fxTime: [],
        temp: []
      },
      updateTime: ""
    };
  },
  computed: {
    //location:function(){
    //  return this.$store.state.location;
    //},
   ...mapState({
      location: 'location'
    })
  },
  methods: {
    // 获取 api 数据
    getData() {
      axios
       .get(API_24H, {
          params: {
            location: this.location.id,
            key: KEY
          }
        })
       .then((res) => {
          console.log(res.data);
          this.hourly = res.data.hourly;
          this.updateTime = dayjs(res.data.updateTime).format("YYYY-MM-DD HH:mm:ss");

          // 优化：直接使用初始化的数组
          this.hour24Data.fxTime = [];
          this.hour24Data.temp = [];

          this.hourly.forEach(element => {
            this.hour24Data.fxTime.push(element.fxTime);
            this.hour24Data.temp.push(element.temp);
          });

          let fxTimeHH = [];
          this.hour24Data.fxTime.forEach(element => {
            fxTimeHH.push(dayjs(element).format("HH"));
          });
          console.log("-----------" + fxTimeHH);

          this.initChart(this.hour24Data.fxTime, this.hour24Data.temp);
        });
    },
    initChart(xAxisData, seriesData) {
      var chartDom = this.$refs.chartContainer;
      var myChart = echarts.init(chartDom);
      var option;
      option = {
        xAxis: {
          type: "category",
          data: xAxisData,
          axisLabel: {
            rotate: 45, // 旋转标签
            textStyle: {
              color: "#666" // 标签颜色
            }
          },
          axisLine: {
            lineStyle: {
              color: "#999" // 轴线颜色
            }
          }
        },
        yAxis: {
          type: "value",
          axisLabel: {
            show: true,
            formatter: "{value}℃",
            textStyle: {
              color: "#666" // 标签颜色
            }
          },
          axisLine: {
            lineStyle: {
              color: "#999" // 轴线颜色
            }
          },
          splitLine: {
            lineStyle: {
              color: ["#ccc"] // 分割线颜色
            }
          }
        },
        series: [
          {
            data: seriesData,
            type: "line",
            lineStyle: {
              color: "#f00" // 折线颜色
            },
            itemStyle: {
              color: "#f00" // 数据点颜色
            }
          }
        ],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow" // 鼠标指针类型
          }
        },
        legend: {
          data: ["温度"],
          textStyle: {
            color: "#666" // 图例文字颜色
          }
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true
        }
      };
      option && myChart.setOption(option);
    }
  },
  mounted() {
    this.getData();
  },
  watch: {
    location: function() {
      this.getData();
    }
  }
};
</script>

<style scoped>
.chart-container {
  height: 20rem;
  width: 100%;
  margin: 0 auto;
}
</style>